package work.content.logfileAnalyze;

import cn.hutool.core.io.FileUtil;
import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Set;

public class Main1 {
    @SneakyThrows
    public static void main(String[] args) {
        BufferedReader logfileReader = new BufferedReader(new InputStreamReader(new FileInputStream(Setting.logfile)));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = logfileReader.readLine()) != null) {
            if (!line.contains(Setting.sql)) {
                continue;
            }
            line = logfileReader.readLine();
            String params = line.split("Parameters: ")[1];
            for (String paramItem : params.split(".(Date.)")) {
                paramItem = paramItem.trim();
                if (paramItem.startsWith("\"")) continue;
                if (paramItem.startsWith(",")) paramItem = paramItem.substring(1).trim();
                paramItem = paramItem.replace("Long", "Integer");
                String[] split = paramItem.split(".(Integer.),");

                String formatted = Setting.sqlItem.formatted(
                        split[0].trim(),
                        split[1].trim(),
                        split[2].trim(),
                        "'" + split[3].trim() + "'"
                );
                sb.append(formatted);
                System.out.println(formatted);

            }
        }

        String sql = Setting.sql + sb.toString();
        FileUtil.writeString(sql, Setting.sqlFile, Charset.defaultCharset());
    }
}
